from django.core.paginator import Paginator
from django.http import Http404
from django.shortcuts import render

from front.DBservice import Myfront, Userservice


def index(request):
    #查询所有文章列表（编号，标题，简介，路径）
    value=request.GET.get('keyboard')
    if value:
        alist = Myfront.getsearch(value)
    else:
        alist = Myfront.getAlist()
    context = {}
    context['alist'] = alist
    return render(request, 'front/index.html', context)

#我的相册
def gallery(request):
    glist = Myfront.myGllery()
    paginator = Paginator(glist, 2)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    context = {'page_obj': page_obj}
    return render(request, 'front/share.html', context)
#我的日记

def articles(request):
    value = request.GET.get('keyboard')
    if value:
        dlist = Myfront.getsear(value)
    else:
        dlist = Myfront.getDlist()
    paginator= Paginator(dlist, 2)
    page_number=request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    context = {'page_obj': page_obj}
    return render(request, 'front/list.html', context)
#关于我
def about(request):
    user = Userservice.aboutme()
    context = {'user': user}
    return render(request, 'front/about.html', context)
#日记详情
def info(request):

    id = request.GET.get('id')
    aobj = Myfront.findById(id)
    if aobj:
        return render(request, 'front/info.html', {'aobj': aobj})
    else:
        raise Http404('查找文章编号{0}不存在'.format(id))
def myfenlei(request):
    id = request.GET.get('id')

    aobj = Myfront.getWZFL()
    if aobj:
        return render(request, 'front/info.html', {'aobj': aobj})
    else:
        raise Http404('查找文章编号{0}不存在'.format(id))

#相册详情
def infopic(request):
    id = request.GET.get('id')
    aobj = Myfront.getinfopic(id)
    if aobj:
        nums = len(aobj.photos)
        context = {'aobj': aobj,'nums':nums}
        return render(request, 'front/infopic.html', context)
    else:
       # raise Http404('相册编号{0}不存在'.format(id))
       return render(request, 'front/infopic.html', {'msg':'该相册下没有照片集'})
#文章分类
def getTypeInfo(request):
    typeName=request.GET.get('type')
    alist=Myfront.getTypelist(typeName)
    return render(request,'front/index.html',{'alist':alist})
